package com.cltolarva;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class CLMonitorList {

	public ArrayList<CLMonitor> allMonitors;

	public CLMonitorList(String clFile) {

		allMonitors = new ArrayList<CLMonitor>();
		this.makeAllMonitor(clFile);
	}

	public void addMonitor(CLMonitor clm) {

		allMonitors.add(clm);
	}

	public void removeMonitor(CLMonitor clm) {

		allMonitors.remove(clm);
	}

	public ArrayList<CLMonitor> getAllMonitors() {

		return this.allMonitors;
	}

	public ArrayList<CLMonitor> getAllActiveMonitors() {

		ArrayList<CLMonitor> temp = new ArrayList<CLMonitor>();

		for (int i = 0; i < this.allMonitors.size(); i++) {

			if (this.allMonitors.get(i).isActive()) {

				temp.add(this.allMonitors.get(i));
			}

		}

		return temp;
	}

	public void makeAllMonitor(String clFile) {

		String[] temp = null;
		try {

			FileInputStream fstream = new FileInputStream(clFile);
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			// Read File Line By Line
			while ((strLine = br.readLine()) != null) {

				temp = strLine.split("@");

				CLMonitor clm = new CLMonitor(temp[1], temp[0], false);

				this.addMonitor(clm);

			}

			in.close();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}

	}

	public CLMonitor getCLMonitor(String MethodName) {

		CLMonitor clm = new CLMonitor("tempContract", "tempMethod", false);
		for (int i = 0; i < this.allMonitors.size(); i++) {

			if (this.allMonitors.get(i).getRelatedMethod().equals(MethodName)) {

				return (CLMonitor) allMonitors.get(i);
			}

		}

		return clm;

	}

	public static void main(String[] args) {

		CLMonitorList ckc = new CLMonitorList("D:\\testcl.txt");

		ckc.allMonitors.get(0).setActive(true);

		ckc.allMonitors.get(3).setActive(true);
		System.out.println(ckc.getAllMonitors().size());

		System.out.println(ckc.getAllActiveMonitors().size());
		
		System.out.print(ckc.getCLMonitor("login").getClContact());

	}

}
